package no.ntnu.item.ttm4120.dhcp.test;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.TreeMap;

public class Runner {

	private static boolean checkRange(InetAddress ip2, int serverPos, boolean takeOver) {
		int start = 1;
		int end = 254;
		int lastPart = ip2.getAddress()[3];
		if(lastPart < 0) lastPart = (lastPart + 256);
			
		System.out.println(start + ", " + end);
		System.out.println(ip2.getAddress()[3]);
		System.out.println("Valid Range:[" + (serverPos*((end - start + 1)/2) + 1) + ", " 
				+ (serverPos*((end - start + 1)/2) + 127) + "]");

		if(lastPart <= (serverPos*((end - start + 1)/2) + 127) &&
				lastPart >= (serverPos*((end - start + 1)/2) + 1)){
			System.out.println("Given address was in rage.");
			return true;
		}else if(takeOver && lastPart <= end && lastPart >= start){
			System.out.println("TakeOver case.");
			return true;
		}else{
			return false;
		}
	}
	public static void main(String[] args) {
		InetAddress ip,ip2;
		try {
			ip = InetAddress.getByAddress(new byte[]{(byte)192, (byte)168, (byte)1, (byte)128});
			System.out.println(ip.getAddress()[3]);
			System.out.println(new String(ip.getAddress()));
			System.out.println(ip);
			System.out.println((ip.getAddress()[3]+1));
			ip2 = InetAddress.getByAddress(new byte[]{
									ip.getAddress()[0],
									ip.getAddress()[1],
									ip.getAddress()[2],
									(byte)(ip.getAddress()[3] + 1)});
			System.out.println(ip2.getHostAddress());
			System.out.println(ip2);
			if(ip.getAddress()[3] == -2) System.out.println("yeap");
//			System.out.println(checkRange(ip, 0, false));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	public static void main1(String[] args) {
//		Runner runner = new Runner();
//		try {
//			InetAddress address = InetAddress.getByAddress(new byte[]{(byte)192, (byte)168, (byte)1, (byte)1});
//			System.out.println(address);
//			InetAddress address2 = InetAddress.getByAddress(new byte[]{address.getAddress()[0],
//					address.getAddress()[1],address.getAddress()[2],
//					(byte)(address.getAddress()[3] + 1)});
//			System.out.println(address2);

			TreeMap<String, String> map = new TreeMap<String, String>();
			byte[] key = new byte[]{(byte)1,(byte)1,(byte)1,(byte)1};
			for (int i = 0; i < 100; i++) {
				key = new byte[]{
						key[0],
						key[1],
						key[2],
						(byte)(key[3] + 1)};
				map.put(new String(key), "client"+i);
			}
			System.out.println(map);
			//			address.getAddress()+1;
//		} catch (UnknownHostException e) {
//			e.printStackTrace();
//		}
	}
}
